package com.avic.rong.wedgit;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import com.avic.rong.R;
import com.avic.rong.utils.ToastTools;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * created by xtj on 2018/8/28 11:23.
 */
public class StringUtil {
    public static boolean isEmpty(String str) {
        if (str == null || "".equals(str)) {
            return true;
        }
        return false;

    }
    /**
     * String 类型 转 int 类型 数字
     *
     * @param numbler
     * @return
     */
    public static int string2int(String numbler) {
        int n = 0;
        if (TextUtils.isEmpty(numbler)) {
            return n;
        }
        try {
            n = Integer.parseInt(numbler);
        } catch (NumberFormatException e) {

            e.printStackTrace();
            n = 0;
        }
        return n;
    }
    /***
     * 根据str2切割str1，保存至list中
     * @param str1 被切割的字符
     * @param str2 切割的字符
     * @return
     */
    public static List<String> stringToList(String str1, String str2){
        if (isEmpty(str1)||isEmpty(str2)){
            return null;
        }
        List<String> list = new ArrayList<String>();
        int i=0;
        while ((i = str1.indexOf(str2)) >= 0) {
            list.add(str1.substring(0, i));
            str1 = str1.substring(i + 1);
        }
        list.add(str1);
        return list;
    }

    /***
     * 将list转换成字符串，中间用str2隔开
     * @param list
     * @param str2
     * @return
     */
    public static String listToString(List<String> list, String str2){
        if (list==null||list.size()<=0){
            return "";
        }
        String str = "";
        for (int i=0;i<list.size();i++){
            if (i==list.size()-1){
                str=str+list.get(i);
            }else{
                str=str+list.get(i)+str2;
            }
        }
        return str;
    }
    public static String replaceEmoji(String str) {
        Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
        Matcher emojiMatcher = emoji.matcher(str);
        return emojiMatcher.replaceAll("");
    }

    static boolean isShowToast = true;

    public static boolean isContainEmoji(Context ctx, String str) {
        Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
        Matcher emojiMatcher = emoji.matcher(str);
        boolean is = emojiMatcher.find();
        if (is && isShowToast) {
            isShowToast = false;
            ToastTools.showToast(ctx, ctx.getResources().getString(R.string.not_support_emoji_from_input_yet));
        }
        new Handler().postDelayed(new Runnable() {
            public void run() {
                isShowToast = true;
            }
        }, 2000);
        return is;
        // return emojiMatcher.replaceAll("");
    }
}
